# A Spam Comment Filter plugin by Hirotaka Ogawa (http://as-is.net/blog/)
#
# Release 0.10 (Jan 28, 2005)
#
# This software is provided as-is. You may use it for commercial or 
# personal use. If you distribute it, please keep this notice intact.
#
# Copyright (c) 2005 Hirotaka Ogawa

use strict;

# Set your spam pattern
our $PATTERN = '<h1>|<a\s'; # H1 or A elements

# Choose one method from: 
# 'CommentFilter', 'CommentThrottleFilter', 'ShortError', 'LongError',
# 'Redirect', 'EvilRedirect'
our $METHOD = 'CommentFilter';

# Set your Redirect URL (for 'Redirect' and 'EvilRedirect' methods)
our $URL = 'http://www.2ch.net/';

if (MT->can('add_plugin')) {
    require MT::Plugin;
    my $plugin = new MT::Plugin();
    $plugin->name("Quasi Spam Filter Plugin, v.0.10");
    $plugin->description("A Simple filter for Comment Spams");
    $plugin->doc_link("http://as-is.net/hacks/2005/01/quasi_spam_filter_plugin.html");
    MT->add_plugin($plugin);
}

if ($METHOD eq 'CommentFilter') {
    MT->add_callback('CommentFilter', 10, 'Reject Spam Comments',
		     \&comment_filter_hdlr);
} elsif ($METHOD eq 'CommentThrottleFilter') {
    MT->add_callback('CommentThrottleFilter', 10, 'Reject Spam Comments',
		     \&comment_throttle_filter_hdlr);
} elsif ($METHOD eq 'ShortError') {
    *MT::App::Comments::pre_run = \&short_error_hdlr;
} elsif ($METHOD eq 'LongError') {
    *MT::App::Comments::pre_run = \&long_error_hdlr;
} elsif ($METHOD eq 'Redirect') {
    *MT::App::Comments::pre_run = \&redirect_hdlr;
} elsif ($METHOD eq 'EvilRedirect') {
    *MT::App::Comments::pre_run = \&evil_redirect_hdlr;
}

sub is_spam {
    my $text = shift;
    return ($text =~ /$PATTERN/i);
}

sub comment_filter_hdlr {
    my ($eh, $app, $comment) = @_;
    return !is_spam($comment->text);
}

sub comment_throttle_filter_hdlr {
    my ($eh, $app, $entry) = @_;
    my $comment = $app->{query}->param('text');
    return !is_spam($comment);
}

sub short_error_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->error("Spam Comment!");
    }
}

sub long_error_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_spam($q->param('text'))) {
        $app->add_methods(post => sub { $_[0]->handle_error("Spam Comments!") });
    }
}

sub redirect_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->redirect($URL);
    }
}

sub evil_redirect_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->redirect($q->param('url') || $URL);
    }
}

1;
